package com.asap.demo.ck.orm;


import com.asap.demo.ck.util.Constants;
import com.asap.demo.ck.util.FuncStatic;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import ru.yandex.clickhouse.ClickHouseDataSource;
import ru.yandex.clickhouse.settings.ClickHouseProperties;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.Map;

public final class JDBCUtils {
	private static final Logger logger = LoggerFactory.getLogger(JDBCUtils.class);

	private static ClickHouseDataSource clickHouseDataSource =null;
	private static  JDBCUtils instance;

	private JDBCUtils(Map<String,String> params) {
		initClickHousePool(params);
	}


	public static JDBCUtils getInstance(Map<String,String> params) {
		if(instance == null) {
			synchronized(JDBCUtils.class) {
				instance = new JDBCUtils(params);
			}
		}

		return instance;
	}

	public void initClickHousePool(Map<String, String> params) {
		String url = params.get(Constants.CK_JDBC_URL);
		String password = params.get(Constants.CK_JDBC_SECRET);
		String user = params.get(Constants.CK_JDBC_USER);
		Integer port = Integer.parseInt(params.getOrDefault(Constants.CK_JDBC_PORT, "9000"));
		if(password.startsWith("ENC(")){
			String algorithm=params.get("jasypt.algorithm");
			String salt=params.get("jasypt.encryptor.password");
			password=password.substring(4, password.length()-1);
			password= FuncStatic.jasyptDecode(algorithm, salt, password);
		}

		ClickHouseProperties clickHouseProperties = new ClickHouseProperties();
		clickHouseProperties.setSocketTimeout(0);
		//clickHouseProperties.setKeepAliveTimeout(3600000);
		clickHouseProperties.setTimeToLiveMillis(3600000);
		clickHouseProperties.setAsync(true);
		clickHouseProperties.setUser(user);
		clickHouseProperties.setPassword(password);
		clickHouseProperties.setPort(port);

		clickHouseDataSource  = new ClickHouseDataSource(url, clickHouseProperties);
	}





	public Connection getClickhouseConnection() {
		Connection conn = null;
		if (null != clickHouseDataSource) {
			try {
				conn = clickHouseDataSource.getConnection();
			} catch (SQLException e) {
				logger.error("getClickhouseConnection",e);
			}
		}
		return conn;
	}

	public void beginTransaction(Connection conn) {
		if(conn != null) {
			try {
				conn.setAutoCommit(false);
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				logger.error("beginTransaction",e);
			}
		}
	}
	

	 
	public void commitTx(Connection conn) {
		if(conn != null) {
			try {
				conn.commit();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				logger.error("commitTx",e);
			}
		}
	}
	

	public void rollBackBusiness(Connection conn) {
		if(conn != null) {
			try {
				conn.rollback();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				logger.error("rollBackBusiness",e);
			}
		}
	}

	public void closeConnect(Connection conn) {
		if(conn != null) {
			try {
				conn.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				logger.error("close connect error",e);
			}
		}
	}
	
}	
